<?php

class Actions extends Table {
	
public static function Pdf($p_uti, $p_todo=TRUE) {
	global $db;
	// Nom de l'utilisateur concerné
	$uti = '';
	$req = "SELECT CONCAT_WS(' ', uti_prenom, uti_nom) as uti_nom
			FROM utilisateurs
			WHERE uti_id='$p_uti'";
	if ( $result = $db->Query( $req ) ){
		if ( $result->num_rows ){
			$row = $result->fetch_assoc();
			$uti = $row['uti_nom'];
		}
	}
	// Requete sql, pr chercher les actions
	$date_fait = ($p_todo)? '': " DATE_FORMAT(act_date_fait, '%d/%m/%Y') as act_date_fait,";
	$null_fait = ($p_todo)? '': ' NOT';
	$req = "SELECT DATE_FORMAT(act_date, '%d/%m/%Y') as act_date,$date_fait rap_objet, CONCAT_WS(', ', S.uti_nom, S.uti_prenom) as source, cli_nom, act_texte, act_date as date_order
			FROM actions
			JOIN rapports ON rapports.rap_id= actions.act_rap_id
			JOIN clients ON clients.cli_id= rapports.rap_cli_id
			JOIN utilisateurs S ON S.uti_id = rapports.rap_uti_id
			WHERE act_uti_id=$p_uti AND act_date_fait IS $null_fait NULL
			ORDER BY date_order";
	if ( $result = $db->Query( $req ) ){
		if ( $result->num_rows ){
			$nb = $result->num_rows;
		}
	}	// Faire ou à faire
	$txt_todo = ($p_todo)? "à faire": "faites";
	// Date du jour
	$today = date("d-m-y");
	// Creation du pdf
	$pdf=new MonFPDFHeader( sprintf( "Actions (%d) %s par %s au %s", $nb, $txt_todo, $uti, $today ) );
	$pdf->AddPage();
	// Impression des actions
	$pdf->SetFont('Arial','',11);
	// hauteur de ligne
	$h = 8;
	while ( $row = $result->fetch_assoc() ){
		$pdf->SetFont('','',12);
		//Date
		$pdf->Write($h,"Pour le ");
		$pdf->SetFont('','B');
		$pdf->Write($h, utf8_decode($row['act_date']));
		if (!$p_todo){
			$pdf->SetFont('','');
			$pdf->Write($h," (fait le ");
			$pdf->SetFont('','B');
			$pdf->Write($h, utf8_decode($row['act_date_fait']));
			$pdf->SetFont('','');
			$pdf->Write($h,' )');
		}
		$pdf->Ln($h);
		// Rapport
		$pdf->SetFont('','B');
		$pdf->Write($h, utf8_decode($row['rap_objet']));
		$pdf->SetFont('','');
		$pdf->Write($h," par ");
		$pdf->SetFont('','B');
		$pdf->Write($h, utf8_decode($row['source']));
		$pdf->SetFont('','');
		$pdf->Write($h," concernant ");
		$pdf->SetFont('','B');
		$pdf->Write($h, utf8_decode($row['cli_nom']));
		$pdf->Ln($h);
		// Texte
		$pdf->SetFont('','',10);
		$pdf->Write($h,utf8_decode($row['act_texte']));
		$pdf->Hr($h);
	}
	// Téléchargement du pdf
	$pdf->Output("Actions $uti $today.pdf",'D');	
}//function Pdf
	
}//class Actions

?>
